﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;

namespace SocketClient {

    /// <summary>
    /// 
    /// </summary>
    public class ReplyItem : IComparable<ReplyItem> {
        public string ReceivedPattern;
        private byte[] ReplyBytes;
        public string Name;
        public string Description;
        public bool Enabled;


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        virtual public byte[] GetReplyBytes() {
            return ReplyBytes;
        }

        virtual public void SetReplyBytes(byte[] reply) {
            this.ReplyBytes = reply;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        virtual public bool Match(string s) {
            if (Enabled) {
                Regex reg = new Regex(ReceivedPattern);
                return reg.IsMatch(s);
            } else {
                return false;
            }
        }

        virtual public bool Match(byte[] bytes) {
            if (bytes == null ||
                bytes.Length == 0) {
                return false;
            }

            string s = (string)HexStringConverter.Default.ConvertToObject(bytes);
            return Match(s);
        }

        public ReplyItem() {

        }

        public ReplyItem(string name, string description, bool enabled, string receivedPattern, byte[] replyBytes) {
            this.Name = name;
            this.Description = description;
            this.Enabled = enabled;
            this.ReceivedPattern = receivedPattern;
            this.ReplyBytes = replyBytes;
        }

        #region ReplyGroup
        /// <summary>
        /// 
        /// </summary>
        public ReplyGroup ReplyGroup {
            get {
                if (_replyGroup == null) {
                    _replyGroup = ReplyGroupManager.Instance["Noname"];
                }
                return _replyGroup;
            }
            set {
                _replyGroup = value;
            }
        } private ReplyGroup _replyGroup;
        #endregion //ReplyGroup


        public int CompareTo(ReplyItem other) {
            return this.Name.CompareTo(other.Name);
        }

    }
}
